import logging
import pickle
from pathlib import Path

from tools.page import page_get
from tools.修改显示金额 import 修改显示金额


def 聚水潭获取(date):
    page = page_get()
    try:
        logging.info('开始获取聚水潭截图')
        page('text=订单').click()
        page.wait(1)
        page.eles('text=订单')[1].click()
        page.wait.ele_displayed('text=组合查询', timeout=20)
        with open(f'D:\\快递赔付\\{date}\\data.pkl', 'rb') as file:
            dataList = pickle.load(file)
        for data in dataList:
            if Path(f'D:\\快递赔付\\{date}\\img\\{str(data[0])}.png').exists():
                continue
            if page.s_eles('#msg_text'):
                page('#msg_close').click()
            page('#by').input(data[6], clear=True)
            page('text=组合查询').click()
            while page.wait.ele_loaded('@@class=wait@@style=display: block;', timeout=0.5):
                pass
            内部订单号 = page('.o_id ').text
            if 内部订单号 in str(data[0]):
                page('.rowList').child(1).child(4).click()
                page.wait(0.2)
                已付金额 = 修改显示金额(page)
                page.get_screenshot(path=f'D:\\快递赔付\\{date}\\img', name=f"{内部订单号}.png", full_page=False,
                                    left_top=(280, 100), right_bottom=(1920, 900))
                data.append(f'D:\\快递赔付\\{date}\\img\\{str(data[0])}.png')
                data.append(已付金额)
            with open(f'D:\\快递赔付\\{date}\\data.pkl', 'wb') as file:
                pickle.dump(dataList, file)
                page.wait(0.5)
        logging.info('聚水潭截图获取完成')
    except Exception as e:
        logging.exception(e)
        page.refresh()
        page.wait(5)
        raise e
